<?php

class Admin_ProductController extends Zend_Controller_Action
{

    public function init()
    {
        $option = array(
          'layout'=>'layout',
          'layoutPath'=> APPLICATION_PATH .'/layouts/scripts/admin',      
        );
        Zend_Layout::startMvc($option);
        $this->_model = new Application_Model_Product();
        $this->_model_manu = new Application_Model_Manufacture();
    }

    public function indexAction()
    {
        $manu = $this->_model_manu->Get_All_Manufacture();
        $this->view->headTitle("Danh sách sản phẩm");
        $this->view->manu = $manu;
    }

    public function addAction()
    {
        try {
            date_default_timezone_set('Asia/Ho_Chi_Minh');
            $form = new Admin_Form_Addproduct();
            $manufacture = $this->_model_manu->Get_All_Manufacture();
            foreach ($manufacture as $item_manu){
                $option[$item_manu->manufacture_id]=$item_manu->manufacture_name;
            }
            $form->manufacture_id->addMultiOptions($option);
            $this->view->form = $form;
            $this->view->headTitle("Thêm sản phẩm mới");
            
            if($this->_request->isPost()){
                if($form->isValid($this->_request->getPost())){
                    $name = $form->getValue("product_name");
                    $manu = $form->getValue("manufacture_id");
                    $price = $form->getValue("product_price");
                    $image = $form->getValue("product_image");
                    $warranty = $form->getValue("product_warranty");
                    $promotion = $form->getValue("product_promotion");
                    $description = $form->getValue("product_description");
                    $date = new Zend_Date();
                    
                    if($form->product_image->isUploaded()){
                        $form->product_image->receive();
                    }
                    $data = array(
                          "product_name"=>$name,
                          "product_price"=>$price,
                          "manufacture_id"=>$manu,
                          "product_image"=>$image,
                          "product_warranty"=>$warranty,
                          "product_promotion"=>$promotion,
                          "product_description"=>$description,
                          "product_dateupdate"=>$date->now('vi_VN')->toString("yyyy-MM-dd"),        
                    );
                    if($this->_model->insert($data))
                        $form->reset();
                    //echo $date;
//                     $this->_helper->url("add", "product", "admin");
                }
//                 date_default_timezone_set('Asia/Ho_Chi_Minh');
//                 $date = new Zend_Date();
//                 echo $date->getDate()->now('vi_VN')->toString("yyyy-MM-dd HH:mm:ss");
//                 echo Zend_Date::now('vi_VN');
            }
        }
        catch (Zend_Exception $ex){
            echo "Error: " .$ex->getMessage();
        }
    }

    public function updateAction()
    {
        //
        $form = new Admin_Form_Addproduct();
        $form->add->setLabel("Cập nhật");
        $manu = $this->_model_manu->Get_All_Manufacture();
        foreach ($manu as $item_manu){
            $options[$item_manu->manufacture_id] = $item_manu->manufacture_name;
        }
        $form->manufacture_id->addMultiOptions($options);
        $this->view->form = $form;
        $this->view->headTitle("Cập nhật sản phẩm");
        //
        
        $product_id = $this->_request->getParam('productid');
        if($product_id){
            $row = $this->_model->find($product_id)->current();
            if(!empty($row)){
                $form->populate($row->toArray());
                $this->view->img = $row->product_image;
                if($this->_request->isPost()){
                    if($form->isValid($this->_request->getPost())){
                        //
                        $row->product_name = $form->getValue('product_name');
                        $row->product_price = $form->getValue('product_price');
                        $row->manufacture_id = $form->getValue('manufacture_id');
                        $row->product_warranty = $form->getValue('product_warranty');
                        $row->product_promotion = $form->getValue('product_promotion');
                        $row->product_description = $form->getValue('product_description');
                        date_default_timezone_set('Asia/Ho_Chi_Minh');
                        $row->product_dateupdate = Zend_Date::now('vi_VN')->toString('yyyy-MM-dd');
                        //
                        if($form->product_image->isUploaded()){
                            $form->product_image->receive();
                            $dir = $form->product_image->getDestination(). '/';
                            //
                            if(is_file($dir.$row->product_image))//delete existing old image
                                unlink($dir.$row->product_image);
                            $row->product_image = $form->getValue('product_image');
                        }
                        $row->save();
                        $this->_helper->redirector('index');
                    }
                }
            }
            else 
                echo 'không có sản phẩm này';
            
        }
    }

    public function deleteAction()
    {
        try {
            $product_id = $this->_request->getParam('id');
            if($product_id){
                $row = $this->_model->find($product_id)->current();
                $result = $row->delete();
                $this->_helper->json = SendJson($result);
            }
            
        }
        catch (Zend_Exception $ex){
            echo 'Error: '.$ex->getMessage();
        }
    }

    public function listallAction()
    {
        try {
            
            $param = $this->_request->getParam("param");
            if($param){
                $keyword = $param['keyword'];
                $manufacture = $param['manu'];
                $select = $this->_model->Search_Product($keyword, $manufacture, 0, 0);
            }
            else 
                $select = $this->_model->Get_All_Product();
            $adapter = new Zend_Paginator_Adapter_DbSelect($select);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(10);
            $current = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($current);
            $this->view->products = $paginator; 
                       
            $this->_helper->layout()->disableLayout();
            $this->_response->clearBody();
        }
        catch (Zend_Exception $ex){
            echo 'Error: ' .$ex->getMessage();
        }
    }

    public function searchAction()
    {
        try{
            $formsearch = new Admin_Form_Searchproduct();
            $manu = $this->_model_manu->Get_All_Manufacture();
            foreach ($manu as $item_manu){
                $option[$item_manu->manufacture_id] = $item_manu->manufacture_name;
            }
            $formsearch->select->addMultiOptions($option);
            $this->view->form = $formsearch;
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
    }


}











